package com.example.mobile2032021606170hsy0507homework;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

public class EngineActivity extends AppCompatActivity {

    private TextView tvEngineTitle;
    private RadioGroup radBtnEngineName;
    private RadioButton radBtnEngine1;
    private RadioButton radBtnEngine2;
    private RadioButton radBtnEngine3;
    private RadioButton radBtnEngine4;
    private RadioButton radBtnEngine5;
    private RadioButton radBtnEngine6;
    private RadioButton radBtnEngine7;
    private RadioButton radBtnEngine8;
    private RadioButton radBtnEngine9;
    private RadioButton radBtnEngine10;
    private RadioButton radBtnEngine11;
    private Button btnEngineSubmit;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_engine);
        initView();
        btnEngineSubmit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                int radBtnId=radBtnEngineName.getCheckedRadioButtonId();
                if (radBtnId!=-1){
                    RadioButton radBtn = findViewById(radBtnId);
                    Intent intent = new Intent();
                    intent.putExtra("Engine",radBtn.getText().toString());
                    setResult(RESULT_OK,intent);
                    finish();
                }else {
                    new AlertDialog.Builder(EngineActivity.this)
                            .setMessage("浏览原材料")
                            .setPositiveButton("添加到购物车",null)
                            .create()
                            .show();
                }
            }
        });
    }

    private void initView() {
        tvEngineTitle = (TextView) findViewById(R.id.tvEngineTitle);
        radBtnEngineName = (RadioGroup) findViewById(R.id.radBtnEngineName);
        radBtnEngine1 = (RadioButton) findViewById(R.id.radBtnEngine1);
        radBtnEngine2 = (RadioButton) findViewById(R.id.radBtnEngine2);
        radBtnEngine3 = (RadioButton) findViewById(R.id.radBtnEngine3);
        radBtnEngine4 = (RadioButton) findViewById(R.id.radBtnEngine4);
        radBtnEngine5 = (RadioButton) findViewById(R.id.radBtnEngine5);
        radBtnEngine6 = (RadioButton) findViewById(R.id.radBtnEngine6);
        radBtnEngine7 = (RadioButton) findViewById(R.id.radBtnEngine7);
        radBtnEngine8 = (RadioButton) findViewById(R.id.radBtnEngine8);
        radBtnEngine9 = (RadioButton) findViewById(R.id.radBtnEngine9);
        radBtnEngine10 = (RadioButton) findViewById(R.id.radBtnEngine10);
        radBtnEngine11 = (RadioButton) findViewById(R.id.radBtnEngine11);
        btnEngineSubmit = (Button) findViewById(R.id.btnEngineSubmit);
    }
}